package com.imstlife.vending.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;

import com.imstlife.vending.activity.MyApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Feng on 2017/3/20.
 */

public class DownLoadTask  extends AsyncTask<String, Integer,File> { //下载地址  进度

    private byte[] current = new byte[2 * 1024];//每次读到的字节数组

    private boolean flag; //是否被取消

    private DownLoadListener listener;

    private Context context;

    public void DownLoadTask(DownLoadListener listener) {

        this.listener = listener;

    }

    //任务执行之前回调
    @Override
    protected void onPreExecute() {
//        listener.onDownLoadStart();
    }

    @Override
    protected File doInBackground(String... params) {//字符串数组
        try {
            URL url = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            float length = connection.getContentLength();
            if (length == 0) {
                return null;
            }
            InputStream is = connection.getInputStream();
            File file = new File("/sdcard/myrunning_apk/");
            if (!file.exists()) {
                file.mkdir();
            }
            String apkPath = "/sdcard/myrunning_apk/myrunning.apk";
            File apkFile = new File(apkPath);
            FileOutputStream fos = new FileOutputStream(apkFile);
            int count = 0;
            byte buf[] = new byte[1024 * 10];
            int size = -1;
            do {
                size = is.read(buf);
                if (size != -1) {
                    fos.write(buf, 0, size);
                    count += size;
                    int progress = (int) (count / length * 100);
                    publishProgress(progress);
                }
            } while (!flag && size != -1);
            fos.close();
            is.close();

            return apkFile;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //更新进度条回调
    @Override
    protected void onProgressUpdate(Integer... values) {
//        listener.onDownLoadProgressChange(values[0]);
        T.showShort(MyApplication.getInstance(), "download:" + values[0] + "%");
    }

    //任务被取消时回调
    @Override
    protected void onCancelled() {
        super.onCancelled();
//        listener.onDownLoadCancle();

    }

    //任务结束后回调
    @Override
    protected void onPostExecute(File file) {
//        listener.onDownLoadFinish();
        if (file == null || file.length() == 0) {
            T.showShort(MyApplication.getInstance(), "download error");
        } else {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            MyApplication.getInstance().startActivity(intent);
        }

    }


    public interface DownLoadListener {

        void onDownLoadStart();

        void onDownLoadProgressChange(int value);

        void onDownLoadCancle();

        void onDownLoadFinish();

    }
}